package com.indusfo.spc.controller;


import com.indusfo.spc.exception.ParamsErrorException;
import com.indusfo.spc.pojo.ExcelErrorData;
import com.indusfo.spc.pojo.Profile;
import com.indusfo.spc.service.ProfileService;
import com.indusfo.spc.untlis.ExcelUtil;
import com.indusfo.spc.untlis.GsonUtil;
import com.indusfo.spc.vo.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author chenm
 * @create 2019-11-26 9:39
 */
@Controller
@RequestMapping("/service/profile")
public class ProfileController  {

  @Autowired
  private ProfileService profileService;

  @RequestMapping("/selectProfile")
  @ResponseBody
  public JSONObject selectProfile(Profile profile){
    return profileService.selectProfile(profile);
  }

  @RequestMapping("/saveProfile")
  @ResponseBody
  public JSONObject saveProfile(@RequestBody Profile profile) {
    try {
      if( null == profile.getMatId()){
        throw new ParamsErrorException("产品id不能为空");
      }
      profileService.saveProfile(profile);
      return JSONObject.oK("保存成功");
    }catch (Exception e){
      e.printStackTrace();
      return JSONObject.build(JSONObject.ERROR,e.getMessage());
    }
  }

  @RequestMapping("/deleteProfile")
  @ResponseBody
  public JSONObject deleteProfile(@RequestParam("profileIds") List<Integer> profileIds){
    if (profileIds.isEmpty()) {
      throw new ParamsErrorException("未传入工艺文件库ID");
    }
    return profileService.deleteProfile(profileIds,2);
  }


  @RequestMapping("/definesProfile")
  @ResponseBody
  public JSONObject definesProfile(@RequestParam("profileIds") List<Integer> profileIds,Integer dataState){
    if (profileIds.isEmpty()) {
      throw new ParamsErrorException("未传入工艺文件库ID");
    }
    return profileService.deleteProfile(profileIds,dataState);
  }


}
